local qimei = fk.CreateSkill{
    name = "ym1__qimei",
}
Fk:loadTranslationTable{
    ["ym1__qimei"] = "齐眉",
    [":ym1__qimei"] = "准备阶段，你可以选择一名其他角色，直至你下回合开始时，双方手牌互相可见，且可以如手牌般使用或打出一次对方的非锦囊手牌。",
    ["#ym1__qimei"] = "是否发动 齐眉，与一名其他角色共享手牌？",
    ["@[ym1__qimei]"] = "齐眉",

    ["$ym1__qimei1"] = "辅车相依，比翼双飞。",
    ["$ym1__qimei2"] = "情投意合，相濡以沫。",
}
Fk:addQmlMark{
    name = "ym1__qimei",
    how_to_show = function(_, value)
        if type(value) ~= "table" then return " " end
        if not value[1] or type(value[1]) ~= "number" then return tostring(#value) end
        local player = Fk:currentRoom():getPlayerById(value[1])
        if (not player) or (not player.general) or (player.general == "") then return tostring(#value) end
        local ret = player.general
        if player.deputyGeneral and player.deputyGeneral ~= "" then
            ret = "seat#" .. player.seat
        end
        return Fk:translate(ret)
    end,
    qml_path = function(name, value, p)
        if Self:isBuddy(p) or not string.startsWith(name, "@[ym1__qimei]$") then
            return "packages/utility/qml/PlayerBox"
        end
        return ""
    end,
}
qimei:addEffect(fk.EventPhaseStart, {
    can_trigger = function (self, event, target, player, data)
        return target == player and player.phase == Player.Start and player:hasSkill(qimei.name)
    end,
    on_cost = function (self, event, target, player, data)
        local tos = player.room:askToChoosePlayers(player, {
            max_num = 1,
            min_num = 1,
            targets = player.room:getOtherPlayers(player),
            skill_name = qimei.name,
            prompt = "#ym1__qimei"
        })
        if #tos > 0 then
            event:setCostData(self, {tos = tos})
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        room:addTableMarkIfNeed(player, "@[ym1__qimei]", to.id)
    end,
})

qimei:addEffect(fk.TurnStart, {
    can_refresh = function (self, event, target, player, data)
        return target == player and player:hasSkill(qimei.name) and type(player:getMark("@[ym1__qimei]")) == "table" and #player:getMark("@[ym1__qimei]") > 0
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        for _, value in ipairs(player:getMark("@[ym1__qimei]")) do
            local tp = room:getPlayerById(value)
            local mark = tp:getTableMark("ym1__qimei-use")
            while table.contains(mark, player.id) do
                table.removeOne(mark, player.id)
            end
            room:setPlayerMark(tp, "ym1__qimei-use", mark)
        end
        room:setPlayerMark(player, "@[ym1__qimei]", 0)
        room:setPlayerMark(player, "ym1__qimei-use", {})
    end,
})

qimei:addEffect("visibility", {
    card_visible = function (self, player, card)
        local owner = Fk:currentRoom():getCardOwner(card.id)
        if owner and table.contains(owner.player_cards[Player.Hand], card.id) and owner ~= player then
            if (type(player:getMark("@[ym1__qimei]")) == "table" and table.contains(player:getMark("@[ym1__qimei]"), owner.id))
            or (type(owner:getMark("@[ym1__qimei]")) == "table" and table.contains(owner:getMark("@[ym1__qimei]"), player.id)) then
                return true
            end
        end
    end,
})

qimei:addEffect("filter",{
    handly_cards = function (self, player)
        local ids = {}
        for _, owner in pairs(Fk:currentRoom().alive_players) do
            if (type(player:getMark("@[ym1__qimei]")) == "table" and table.contains(player:getMark("@[ym1__qimei]"), owner.id))
            or (type(owner:getMark("@[ym1__qimei]")) == "table" and table.contains(owner:getMark("@[ym1__qimei]"), player.id)) then
                if not table.contains(owner:getTableMark("ym1__qimei-use"), player.id) then
                    table.insertTableIfNeed(ids, owner.player_cards[Player.Hand])
                end
            end
        end
        ids = table.filter(ids, function (cardID)
            return Fk:getCardById(cardID).type ~= Card.TypeTrick
        end)
        return ids
    end,
})

local spec = {
    can_refresh = function (self, event, target, player, data)
        return target == player and #table.filter(Card:getIdList(data.card),function (id)
            local owner = player.room:getCardOwner(id)
            return owner and owner ~= player and ((type(player:getMark("@[ym1__qimei]")) == "table" and table.contains(player:getMark("@[ym1__qimei]"), owner.id))
            or (type(owner:getMark("@[ym1__qimei]")) == "table" and table.contains(owner:getMark("@[ym1__qimei]"), player.id)))
        end) > 0
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        for index, id in ipairs(Card:getIdList(data.card)) do
            local owner = room:getCardOwner(id)
            if owner and owner ~= player and ((type(player:getMark("@[ym1__qimei]")) == "table" and table.contains(player:getMark("@[ym1__qimei]"), owner.id))
            or (type(owner:getMark("@[ym1__qimei]")) == "table" and table.contains(owner:getMark("@[ym1__qimei]"), player.id))) then
                if type(player:getMark("@[ym1__qimei]")) == "table" and table.contains(player:getMark("@[ym1__qimei]"), owner.id) then
                    room:notifySkillInvoked(player, qimei.name,"special")
                    player:broadcastSkillInvoke(qimei.name)
                else
                    room:notifySkillInvoked(owner, qimei.name,"special")
                    owner:broadcastSkillInvoke(qimei.name)
                end
                room:addTableMarkIfNeed(room:getPlayerById(owner.id), "ym1__qimei-use", player.id)
            end
        end
    end,
}

qimei:addEffect(fk.PreCardUse, spec)
qimei:addEffect(fk.PreCardRespond, spec)

return qimei